/** 
 * File-Name:ArticleAction.java
 *
 * Created on 2011-6-2 下午10:10:52
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.web;

import java.io.File;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import neoutil.CommonAction;
import neoutil.CommonService;
import neoutil.Config;
import neoutil.FileUtils;
import neoutil.Page;

import org.apache.log4j.Logger;
import org.quartz.jobs.FileScanJob;

import site.entity.Article;
import site.entity.Category;
import site.entity.Paper;
import site.entity.Resource;
import site.entity.User;
import site.helper.CommonHelper;
import site.helper.CourseTitle;
import site.helper.ResourceType;
import site.helper.TableHeader;
import site.helper.TableHeaderFactory;
import site.service.ArticleRepository;
import site.service.CategoryRepository;

/**
 * @type PaperAction
 * @info author:陈亚鹏  date:2012-10-22 下午04:50:23 version:1.0
 */
@SuppressWarnings("serial")
public class PaperAction extends CommonAction {
	private static Logger log = Logger.getLogger(PaperAction.class);
	private ArrayList<List<Paper>> lstPapers = new ArrayList<List<Paper>>();
	private int cid;
	private Category category;
	private List<Category> categories;	
	private Page<Paper> page = new Page<Paper>(15);	
	private List<TableHeader> tableHeaders = null;
	private Paper paper;
	private String editOrAdd = "edit";
	private File paperFile;
	private String paperFileFileName;
	private String paperFileContentType;
	private String type;

	public String viewPapers()
	{
		cid = Integer.parseInt(getRequestParameter("cid"));
		CategoryRepository categoryRepository = getCategoryRepository();
		category = categoryRepository.getCategoryByCid(cid);
		categories = categoryRepository.getCategoriesByParentCid(category
				.getParentCid());
					
		for(String t : new String[]{CourseTitle.ZL,CourseTitle.YLK,CourseTitle.JCK,CourseTitle.GLK,CourseTitle.GYK})
		{
			List<Paper> papers = getPaperRepository().getLatestPapersByCidAndType(cid,CourseTitle.ToCode(t),10);
			lstPapers.add(papers);
		}			
		return SUCCESS;
	}
	
	public String viewMorePapers()
	{
		cid = Integer.parseInt(getRequestParameter("cid"));
		CategoryRepository categoryRepository = getCategoryRepository();
		category = categoryRepository.getCategoryByCid(cid);
		categories = categoryRepository.getCategoriesByParentCid(category
				.getParentCid());
		
		type = getRequestParameter("type");
		page = getPaperRepository().getPapersByCidAndType(page, cid,type);
		tableHeaders = TableHeaderFactory.getPaperTableHeaders();
		return SUCCESS;
	}
	
	public String viewPaperByUuid()
	{
		paper = getPaperRepository().getPaperByUuid(getRequestParameter("uuid"));		
		CategoryRepository categoryRepository = getCategoryRepository();
		category = categoryRepository.getCategoryByCid(paper.getCid());
		categories = categoryRepository.getCategoriesByParentCid(category
				.getParentCid());
		return SUCCESS;
	}
	
	public String managePapers()
	{
		cid = Integer.parseInt(getRequestParameter("cid"));
		tableHeaders = TableHeaderFactory.getManagePaperTableHeaders();
		page = getPaperRepository().getPapersByCidAndType(page, cid,null);
		return SUCCESS;
	}
	
	public String editOrAddPaper()
	{
		cid = Integer.parseInt(getRequestParameter("cid"));
		CategoryRepository categoryRepository = getCategoryRepository();
		category = categoryRepository.getCategoryByCid(cid);
		String uuid = getRequestParameter("uuid");
		if(uuid != null){
			paper = getPaperRepository().getPaperByUuid(uuid);
			if(!CommonHelper.IsNullOrEmpty(paper.getFilePath())){
				paperFile = new File(Config.getPaperSavePath() + paper.getFilePath());
				paperFileFileName = "dasda";
				paperFileContentType="image/jpeg";
			}
			editOrAdd = "edit";
		}
		else
		{
			paper = new Paper();
			paper.setCid(cid);
			editOrAdd = "add";
		}
		
		return SUCCESS;
	}

	public String saveOrUpdatePaper()
	{
		if (paperFile != null) {
			String fileName = paper.getTitle()
					+ FileUtils.getExtention(getPaperFileFileName());
			FileUtils.copy(getPaperFile(),
					new File(Config.getPaperSavePath() + fileName));

			paper.setFilePath(fileName);
		}
		if(editOrAdd.equals("add"))
		{
			getPaperRepository().save(paper);
		}
		else
		{
			getPaperRepository().update(paper);
		}
		
		return SUCCESS;
	}
	
	public String deletePaperByUuid()
	{
		cid = Integer.parseInt(getRequestParameter("cid"));
		String uuid = getRequestParameter("uuid");
		paper = getPaperRepository().getPaperByUuid(uuid);
		getPaperRepository().deletePaper(paper);
		return SUCCESS;
	}
	
	public String downloadPaperEnclosure() {
		String uuid = getRequestParameter("uuid");
		paper = getPaperRepository().getPaperByUuid(uuid);

		if (paper != null
				&& paper.getFilePath() != null
				&& new File(Config.getPaperSavePath() + paper.getFilePath())
						.exists()) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}	

	public InputStream getEnclosureFile() {
		try {
			String uuid = getRequestParameter("uuid");
			paper = getPaperRepository().getPaperByUuid(uuid);
			String URL = Config.getPaperSavePath() + paper.getFilePath();
			paper.setFilePath(paper.getTitle() + "." + CommonHelper.getExtension(URL));
			paper.setFilePath(URLEncoder.encode(paper.getFilePath(),"UTF-8"));

			return new java.io.FileInputStream(URL);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void setTableHeaders(List<TableHeader> tableHeaders) {
		this.tableHeaders = tableHeaders;
	}

	public List<TableHeader> getTableHeaders() {
		return tableHeaders;
	}
	public ArrayList<List<Paper>> getLstPapers() {
		return lstPapers;
	}

	public void setLstPapers(ArrayList<List<Paper>> lstPapers) {
		this.lstPapers = lstPapers;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public void setPage(Page<Paper> page) {
		this.page = page;
	}

	public Page<Paper> getPage() {
		return page;
	}

	public Paper getPaper() {
		return paper;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}

	public void setEditOrAdd(String editOrAdd) {
		this.editOrAdd = editOrAdd;
	}

	public String getEditOrAdd() {
		return editOrAdd;
	}

	public void setPaperFile(File paperFile) {
		this.paperFile = paperFile;
	}

	public File getPaperFile() {
		return paperFile;
	}

	public void setPaperFileContentType(String paperFileContentType) {
		this.paperFileContentType = paperFileContentType;
	}

	public String getPaperFileContentType() {
		return paperFileContentType;
	}

	public void setPaperFileFileName(String paperFileFileName) {
		this.paperFileFileName = paperFileFileName;
	}

	public String getPaperFileFileName() {
		return paperFileFileName;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getType() {
		return type;
	}

}
